package com.example.smartcity_poverty.ui.AllService.DataAnalysis

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.R
import com.github.mikephil.charting.charts.HorizontalBarChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.utils.ViewPortHandler
import kotlinx.android.synthetic.main.activity_data_analysis.*

/**
 * 数据分析.
 */
class DataAnalysisActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_data_analysis)
        title = "数据分析"


        // 折线图
        setBarChar1()

        // 显示柱形图
        setBarChart2()

        // 横向条形图
        sethBarChart()

        // 拼图
        setPieChart()
    }

    /**
     * Pie图
     */
    private fun setPieChart() {
        val list = ArrayList<PieEntry>().apply {
            for (i in 1..10) {
                this.add(PieEntry(i.toFloat(), "新闻$i"))
            }
        }
        val ds = PieDataSet(list, "111")
        val barData = PieData(ds)
        pinBarChat.data = barData

        pinBarChat.animateX(1000)
        pinBarChat.animateY(1500)
    }


    /**
     * 横向条形图
     */
    private fun sethBarChart() {
        val list = ArrayList<BarEntry>().apply {
            for (i in 1..7) {
                val num = (1..6000).random()
                this.add(BarEntry(i.toFloat(), num.toFloat()))
            }
        }

        val hbc = BarDataSet(list, "啊哈哈哈")
        val barData = BarData(hbc)
        hBarChart.apply {
            this.data = barData
            this.xAxis.position = XAxis.XAxisPosition.BOTTOM
            this.animateY(2000)
            this.animateX(3000)
            this.xAxis.textColor = Color.BLUE
            this.xAxis.valueFormatter = object : IndexAxisValueFormatter() {

                override fun getBarLabel(barEntry: BarEntry?): String {
                    if (barEntry?.x == 1.toFloat()) {
                        return "212112"
                    }
                    return ""
                }
            }
        }
    }

    /**
     * 折线图
     */
    private fun setBarChar1() {
        // 展示的数据
        val list = ArrayList<Entry>().apply {
            for (i in 1..7) {
                val num = (1..6000).random()
                this.add(Entry(i.toFloat(), num.toFloat()))
            }
        }
        val line = LineDataSet(list, "客流量")
        val lineData = LineData(line)
        dataBarChar.data = lineData
        dataBarChar.xAxis.valueFormatter = object : IndexAxisValueFormatter() {
            val str = listOf<String>(
                "2020-4-21日",
                "2020-4-22日",
                "2020-4-23日",
                "2020-4-24日",
                "2020-4-25日",
                "2020-4-26日",
                "2020-4-27日"
            )

            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return str.getOrNull(value.toInt()) ?: value.toString()
            }
        }
        dataBarChar.animateX(1000)
        dataBarChar.animateY(1500)
    }


    /**
     * 柱形图
     */
    private fun setBarChart2() {
        // 存储坐标数据
        val array = ArrayList<BarEntry>().apply {
            for (i in 1..7){
                val num = (999..3000).random()
                this.add(BarEntry(i.toFloat(), num.toFloat()))
            }
        }
        val set1 = BarDataSet(array, "客运周转量")
        val dataSet = ArrayList<IBarDataSet>()
        dataSet.add(set1)
        val dataBar = BarData(dataSet)
        charBar.data = dataBar
        // 执行动画
        charBar.animateY(1500)
        charBar.animateX(2100)
        charBar.invalidate() // 显示出来

        charBar.xAxis.valueFormatter = object : IndexAxisValueFormatter() {
            val str = listOf<String>("新闻1", "新闻2", "新闻3", "新闻4", "新闻5", "新闻6", "新闻7")
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                return str.getOrNull(value.toInt()) ?: value.toString()
            }

            override fun getBarLabel(barEntry: BarEntry?): String {
                return super.getBarLabel(barEntry)
            }
        }


    }



    inner class ValueFormat : ValueFormatter() {
        override fun getFormattedValue(value: Float): String {
            return (value.toInt() * 100).toString() + "人"
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}